package com.ruoyi.stuclient.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.stuclient.domain.EwcHomeworkSubmission;
import com.ruoyi.stuclient.domain.dto.HomeworkSubmitDto;
import com.ruoyi.stuclient.service.IEwcHomeworkSubmissionMaterialsService;
import com.ruoyi.stuclient.service.IEwcHomeworkSubmissionService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 * 课后作业提交Controller
 * 
 * @author zsp
 * @date 2025-05-08
 */
@Validated
@RestController
@RequestMapping("/api/homework")
public class EwcHomeworkSubmissionController extends BaseController
{
    @Autowired
    private IEwcHomeworkSubmissionService ewcHomeworkSubmissionService;

    @Autowired
    private IEwcHomeworkSubmissionMaterialsService ewcHomeworkSubmissionMaterialsService;

    /**
     * 提交课后作业
     */
    @Operation(summary = "学生端我的页面提交课后作业")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/homeworkSubmit")
    public AjaxResult getHomeworkListInfo(@Valid @RequestBody HomeworkSubmitDto homeworkSubmitDto)
    {
        //清除提交记录
        EwcHomeworkSubmission ewcHomeworkSubmission = ewcHomeworkSubmissionService.getSubmissionHomework(homeworkSubmitDto.getHomeworkId());
        if(Objects.nonNull(ewcHomeworkSubmission)){
            Boolean deleteMaterials = ewcHomeworkSubmissionMaterialsService.deleteSubmissionMaterials(ewcHomeworkSubmission.getHomeworkSubmissionId());
            if(deleteMaterials){
                try {
                    Boolean deleteSub = ewcHomeworkSubmissionService.deleteSubmissionHomework(homeworkSubmitDto.getHomeworkId());
                    if(!deleteSub){
                        throw new GlobalException("提交失败");
                    }
                } catch (Exception e) {
                    throw new GlobalException("提交失败");
                }
            }
        }
        //创建提交
        boolean result = false;
        String submissionId = "";
        //1.课后作业提交表入库
        submissionId = ewcHomeworkSubmissionService.homeworkSubmission(homeworkSubmitDto);
        //2.作业提交资料表入库
        if(!Objects.equals("",submissionId)){
            homeworkSubmitDto.setHomeworkSubmissionId(submissionId);
            try {
                result = ewcHomeworkSubmissionMaterialsService.homeworkSubmissionMaterials(homeworkSubmitDto);
            } catch (Exception e) {
                throw new GlobalException("提交失败");
            }
        }
        if(result){
            return AjaxResult.success("提交成功");
        }
        return AjaxResult.error("提交失败");
    }

}
